﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace SurehealthForms.Pharmacistfolder
{
    public partial class ReturnStock : Form
    {
            List<string> Items = new List<string>();
            List<CashierLogin.perscriptionItem> PerscriptionItems = new List<CashierLogin.perscriptionItem>();
            List<string>RawItems = new List<string>();
            List<XmlDocument> Prescriptions = new List<XmlDocument>();
            public ReturnStock()
            {
                InitializeComponent();
            }

            private void ReturnStock_Load(object sender, EventArgs e)
            {
                StreamReader Reader = new StreamReader("PerscriptionsForCollection.txt");
                while (Reader.EndOfStream == false)
                {
                    string tempString = Reader.ReadLine();
                    AvailablePrescriptions.Items.Add(ConvertNumbtoXML(tempString));
                    RawItems.Add(tempString);
                }

                Reader.Close();
                Reader.Dispose();
            }
        
            private string ConvertNumbtoXML(string Numb)
            {
                XmlDocument document = null;
                document = new XmlDocument();
                document.Load(Numb + ".Xml");
                System.Xml.XmlElement root = document.DocumentElement;
                string patientname = root.GetAttribute("Name");
                string DateSet = root.GetAttribute("Date");
                Items.Add(Numb);

                return "(" + Numb+ ")\t " + patientname + "\t\t" + DateSet;
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                if (AvailablePrescriptions.SelectedIndex > -1)
                {
                    
                    Items.RemoveAt(AvailablePrescriptions.SelectedIndex);
                    AvailablePrescriptions.Items.Remove(AvailablePrescriptions.SelectedItem);



                    StreamWriter Writer = new StreamWriter("PerscriptionsForCollection.txt");

                    foreach (string s in Items)
                    {
                        Writer.WriteLine(s);
                    }

                    Writer.Close();
                    Writer.Dispose();
                }
                else
                {
                    MessageBox.Show("You need to select a Item");
                }
            }

            private void ReturnStockList_SelectedIndexChanged(object sender, EventArgs e)
            {

            }


            private void ConfirmPickUp_Click(object sender, EventArgs e)
            {
                if (AvailablePrescriptions.SelectedIndex != -1)
                {
                    string file = AvailablePrescriptions.SelectedItem.ToString();
                    char[] stringfilearray = file.ToCharArray();
                    int Start = 0, End = 0;

                    int i = 0;
                    foreach (char c in stringfilearray)
                    {
                        if (c == '(')
                            Start = i;
                        if (c == ')')
                            End = i;

                        i++;
                    }

                    string join = "";
                    for (int iNumb = Start + 1; iNumb < End; iNumb++)
                    {
                        join = join + stringfilearray[iNumb];
                    }

                    
                        Items.RemoveAt(AvailablePrescriptions.SelectedIndex);
                        AvailablePrescriptions.Items.Remove(AvailablePrescriptions.SelectedItem);

                        StreamWriter Writer = new StreamWriter("PerscriptionsForCollection.txt");
                        foreach (string s in Items)
                        {
                            Writer.WriteLine(s);
                        }
                        Writer.Close();

                        //load the right document
                        XmlDocument document = null;
                        document = new XmlDocument();
                        document.Load(join + ".Xml");//Blah
                        System.Xml.XmlElement root = document.DocumentElement;

                        string Doctorname = root.GetAttribute("Doctor");
                        string Discount = root.GetAttribute("FullDiscount");

                        XmlNodeList nodes = document.GetElementsByTagName("PrescriptionITEM");
                        foreach (XmlNode n in nodes)
                        {
                            XmlElement Item = document.DocumentElement;
                            string Quantity = n.Attributes["Quantity"].InnerText;
                            string StockItem = n.Attributes["SItem"].InnerText;

                            UpdateItemSales(Quantity, StockItem);

                            UpdateSpendature(root, Doctorname, Quantity, StockItem);

                            updateTotalSales(Discount, Quantity, StockItem);

                            updateWeeklySales(Quantity);
                        }

                }
                else
                {
                    MessageBox.Show("You need to select a Item");
                }
            }

            private static void updateTotalSales(string PDiscount, string PQuantity, string PStockItem)
            {
                if (PDiscount != "Checked")
                {
                    StreamReader Reader3 = new StreamReader("TotalSales.txt");

                    int Quantity = int.Parse(PQuantity);
                    decimal cost = DodgyBobStockControl.Stock.StockQueryCost(PStockItem);
                    decimal currentSales = decimal.Parse(Reader3.ReadLine());
                    string newtotalsales = ((Quantity * cost) + currentSales).ToString();
                    Reader3.Close();
                    StreamWriter Writer3 = new StreamWriter("TotalSales.txt");
                    Writer3.WriteLine(newtotalsales);
                    Writer3.Close();
                }
            }

            private static void updateWeeklySales(string pQuantity)
            {
                StreamReader WeekTotalSales = new StreamReader("WeeklyTotalSales.txt");

                int quantity = int.Parse(pQuantity);
                DateTime DateOfLastReset = DateTime.Parse( WeekTotalSales.ReadLine());
                int CurrentQuantity = int.Parse(WeekTotalSales.ReadLine());
                int newQuantity = CurrentQuantity + quantity;
                WeekTotalSales.Close();

                TimeSpan diff =  DateTime.Now - DateOfLastReset;
                int diffInDays = diff.Days;
                string NewDate = DateOfLastReset.ToString("d");
                if (diffInDays >= 7)
                    NewDate = DateTime.Now.ToString("d");

                StreamWriter WeekTotalSalesWriter = new StreamWriter("WeeklyTotalSales.txt");
                WeekTotalSalesWriter.WriteLine(NewDate);
                WeekTotalSalesWriter.WriteLine(newQuantity);
                WeekTotalSalesWriter.Close();

            }

            private static void UpdateSpendature(System.Xml.XmlElement root, string Doctorname, string Quantity, string StockItem)
            {
                XmlDocument document2 = null;
                document2 = new XmlDocument();
                document2.Load(Doctorname + ".XML");
                System.Xml.XmlElement root2 = document2.DocumentElement;
                string Spendature = root2.InnerText;
                decimal ItemCost = DodgyBobStockControl.Stock.StockQueryCost(StockItem);
                decimal Quantityd = decimal.Parse(Quantity);
                string Spendatureupdate="";
                if (Spendature== "0.00")
                    Spendatureupdate = (Quantityd * ItemCost).ToString();
                else
                    Spendatureupdate = (decimal.Parse(Spendature) + (Quantityd * ItemCost)).ToString();

                string filename = Doctorname + ".Xml";

                XmlTextWriter Writer2;
                Writer2 = new XmlTextWriter(filename, Encoding.ASCII);
                Writer2.Formatting = Formatting.Indented;
                Writer2.WriteStartDocument();
                Writer2.WriteStartElement("Doctor");
                Writer2.WriteAttributeString("Name", Doctorname);
                Writer2.WriteAttributeString("PatientFileName", Doctorname + "patients.txt");
                Writer2.WriteElementString("Spendature", Spendatureupdate);
                Writer2.WriteEndDocument();
                Writer2.Close();
            }

            private static void UpdateItemSales(string Quantity, string StockItem)
            {

                StreamReader Reader = new StreamReader(@"Items\\" + StockItem + ".txt");
                string newItemSales = (int.Parse(Quantity) + int.Parse(Reader.ReadLine())).ToString();
                Reader.Close();
                StreamWriter Writer = new StreamWriter(@"Items\\" + StockItem + ".txt");
                Writer.WriteLine(newItemSales);
                Writer.Close();
            }
    
        }


}


